home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7648 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  100 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const char??
  5. Date: Tue, 27 Feb 96 20:47:19 GMT
  6. Organization: none
  7. Message-ID: <825454039snz@genesis.demon.co.uk>
  8. References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com> <TANMOY.96Feb20095538@qcd.lanl.gov> <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >        int * const x;
  18. >
  19. >The trick is to see the declaration of the type, and the further declarators of
  20. >objects derived from the type as separate syntactic units. Immediate
  21. >understanding is thereafter forthcoming. :)
  22.  
  23. I'll take your word on that! :-)
  24.  
  25. > >(There is a related issue that the function may also change *x by
  26. > >using the lvalue *(char*)x. That, and similar tricks, are so bad
  27. > >programming practices, that I won't even comment on them!)
  28. >
  29. >Hmm. Is that not a bit of an error, since the const modifier is being dropped
  30. >in the cast? C allows you to only safely convert pointers of any type to void *
  31. >and back to the same type. I will have to check references whether or not that
  32. >"type" is so restrictive as to include any modifiers like const and volatile.
  33.  
  34. 6.3.4 
  35.  
  36. "It is guaranteed, however, that a pointer to an object of a given alignment
  37.  may be converted to a pointer to an object of the same alignment or less
  38.  strict alignment and back again; the result shall compare equal to original
  39.  pointer."
  40.  
  41. 6.1.2.5
  42.  
  43. "The qualified or unqualified versions of a type are distinct types that
  44.  belong to the same type category and have the same representation and
  45.  alignment requirements."
  46.  
  47.  
  48. So you can certainly cast between pointers to differently qualified versions
  49. of the same type and back again. More simply I take:
  50.  
  51. 6.3.4
  52.  
  53. "A pointer to an object or incomplete type may be converted to a pointer
  54.  to a different object type or a different incomplete type. The resulting
  55.  pointer might not be valid if it is improperly aligned for the type
  56.  pointed to."
  57.  
  58. to mean that if the result *is* properly aligned (as in this case) then
  59. the result is valid.
  60.  
  61. >In any case, the cast does make it explicit that you are trying to voluntarily
  62. >break the const. But in your function declaration you promised that you would
  63. >not touch the object---why go through the bother of making that promise to the
  64. >caller, when you intend to violate it?
  65.  
  66. The cast doesn't necessarily violate the promise. Consider writing an
  67. implementation of strchr() in C. The prototype is:
  68.  
  69. char *strchr(const char *s, int c);
  70.  
  71. It returns a pointer derived from s or NULL. Somewhere within this function
  72. you are going to have to convert from const char * to char *. This function
  73. does break the 'chain of constness' and if you pass it a const char * value
  74. you should ensure that you assign the return value to a const char * variable.
  75. To get around this strchr() would have had to return an offset rather than
  76. a pointer.
  77.  
  78.  
  79. > >An object _defined_ a (i.e. an object whose storage was allocated by a
  80. > >declaration specifying) const, cannot be portably changed by the
  81. > >program. A compiler can assume it's value won't change unless it is
  82. > >also volatile: in which case, even though the program cannot write to
  83. > >it, the compiler has to assume that its value can change due to unkwon
  84. > >reasons. 
  85. >
  86. >A read-only device status register would be an example of such a "volatile
  87. >const" beast, no?
  88.  
  89. Yes. Also an object shared by different programs could be volatile in one
  90. and const volatile in another. I can't see much use for const volatile
  91. in a strictly conforming program however since C itself can't create objects
  92. for which such qualification would make sense (unless you can think of
  93. something!)
  94.  
  95. -- 
  96. -----------------------------------------
  97. Lawrence Kirby | fred@genesis.demon.co.uk
  98. Wilts, England | 70734.126@compuserve.com
  99. -----------------------------------------
  100.